package ClientControler;


import entities.*;
import java.io.IOException;
import java.util.Observable;
import java.util.Observer;

import ClientCont.ChatClient;
import ocsf.client.ObservableSWRClient;


public class LoginController implements Observer {

	/** The client. */
	ChatClient client;

	/** The user permission. */
	int userPermission = 0 ; 
	
	/**
	 * Creates a new connection.
	 */
	
	public LoginController()
	{
	System.out.println("LoginController created.");
	client = new ChatClient();
	client.addObserver(this);
   
	}
	
	/**
	 * Disconnect.
	 */
	public void disconnect()
	{
		try
		{
			client.closeConnection();
		}
		catch(IOException e) {}
	}
	
	/**
	 * Connect.
	 *
	 * @param ip the ip
	 * @param port the port
	 * @return true, if successful
	 */
	public boolean connect(String ip,String port)
	{
		System.out.println("hello");
		int portnum;
		// Check port
		try
		{
			portnum=Integer.parseInt(port);
		}
		catch (NullPointerException e)
		{
			return false;
		}
		catch (NumberFormatException e)
		{
			return false;
		}
		if ((ip.isEmpty()) || (port.isEmpty())) 
			return false;
		if ((portnum < 1024) || (portnum > 65535)) 
			return false;
		// Check IP 
	//	if ( validateIP(ip) == false ) 
	//		return false;
		
		// Start connection to server
	    try 
	    {
	    	client.startConnection(ip,portnum);
	    	return true;
	    } 
	    catch(IOException exception)
	    {  
	    	System.out.println("NO START CONNECTION");
	    	return false;
	    }
	}
	 
	/**
	 * Login.
	 *
	 * @param user the user
	 * @param pass the pass
	 * @return the system user
	 */
	public SystemUser login(String user,String pass,int type)
	{
		if (type==0){
			
			Client sysUser = new Client(user,pass);
			sysUser.setMsgType(MessageManager.msgType.LOGIN_REQUEST);
			try 
			{
				System.out.println("Sending LOGIN & WAITING..");
				client.setMaxRetries(2);
				client.setWaitTime(3000);
				Object return_obj = null;
				return_obj = client.sendToServerAndWait(sysUser,sysUser);
				sysUser=(Client) return_obj;
				if (return_obj==null){
						return null;
				}
				else{
					System.out.println("DBCTRLR: login result = " + sysUser.getMsgResponse());
					return sysUser;
				}
			}catch (Exception e){
				return null;
				}
		}
		else{
			
			Employee sysUser = new Employee(user,pass);
			sysUser.setMsgType(MessageManager.msgType.LOGIN_REQUEST);
			try 
			{
				System.out.println("Sending LOGIN & WAITING..");
				client.setMaxRetries(2);
				client.setWaitTime(3000);
				Object return_obj = null;
				return_obj = client.sendToServerAndWait(sysUser,sysUser);
				sysUser=(Employee) return_obj;
				if (return_obj==null){
						return null;
				}
				else{
					System.out.println("DBCTRLR: login result = " + sysUser.getMsgResponse());
					return sysUser;
				}
			}catch (Exception e){
				return null;
				}
		}
		 
	}
	
	/**
	 * Logout.
	 *
	 * @param sysUser the system user
	 * @return the system user
	 */
	public SystemUser logout(SystemUser sysUser)
	{
		 sysUser.setMsgType(MessageManager.msgType.LOGOUT_REQUEST);	
			try 
			{
				System.out.println("Sending LOGOUT & WAITING..");
				client.setMaxRetries(2);
				client.setWaitTime(3000);
				Object return_obj = null;
				MessageManager.msgResponse type;
				type=null;
				return_obj = client.sendToServerAndWait(sysUser,type);
				sysUser=(SystemUser) return_obj;
				if (return_obj==null)
				{
					return null;
				}
				else
				{
					System.out.println("DBCTRLR: logout result = " + sysUser.getMsgResponse());
					return sysUser;
				}
			} 
			catch (Exception e) 
			{
				
				return null;
			}
		// return 1;
		 
	}
	
	/**
	 * Gets the client.
	 *
	 * @return the client
	 */
	public ChatClient getClient()
	{
		return client;
	}
	

	@Override
	public void update(Observable arg0, Object arg1) 
	{
		System.out.println("LoginController: Recieved notify: arg0:   " + arg0.toString() + "   |   arg1: "+ arg1.toString());
		if (arg1 == ObservableSWRClient.CONNECTION_CLOSED)
		{
			System.out.printf("LoginController: Connection closed notify.");
			if (client.isConnected())
			{
				System.out.printf("Closing connection..");
				try 
				{
					client.closeConnection();
				} 
				catch (IOException e) {}
			}
		}
		
		if (arg1 == ObservableSWRClient.WAITING_FOR_REPLY) 
		{
			if (client.getWaitRetries() == client.getMaxRetries())
			{
				System.out.println("Canceling operation with server.");
				client.cancel();
				client.setWaitRetries(0);
			}
		}
	}

}
